#include "ringqueue.hpp"
#include <time.h>
#include "task.hpp"
#include <string.h>
void *customer(void *arg)
{
    // RingQueue<int>* ring=static_cast<RingQueue<int>*>(arg);
    RingQueue<Task> *ring = static_cast<RingQueue<Task> *>(arg);
    while (true)
    {
        /*int data=0;
        ring->pop(&data);
        cout<<"customer:"<<data<<endl;*/

        Task t;
        ring->pop(&t);
        t();
        cout << "customer:" << t.to_customer() << endl;
    }
}

void *productor(void *arg)
{
    // RingQueue<int>* ring=static_cast<RingQueue<int>*>(arg);
    RingQueue<Task> *ring = static_cast<RingQueue<Task> *>(arg);
    while (true)
    {
        // sleep(3);
        /*int data=rand()%20;
        ring->push(data);
        cout<<"productor:"<<data<<endl;*/

        sleep(2);
        int x = rand() % 20;
        int y = rand() % 10;
        char *arr = "+-*/%";
        char c = arr[rand() % strlen(arr)];
        Task t(x, y, c);
        ring->push(t);
        cout <<"prodcutor:" <<t.to_productor() << endl;
    }
}

int main()
{
    srand(time(nullptr));
    pthread_t t1_productor;
    pthread_t t2_customer;
    // RingQueue<int>* rq=new RingQueue<int>();
    RingQueue<Task> *rq = new RingQueue<Task>();
    pthread_create(&t1_productor, nullptr, productor, rq);
    pthread_create(&t2_customer, nullptr, customer, rq);
    pthread_join(t1_productor, nullptr);
    pthread_join(t2_customer, nullptr);
    delete rq;
    return 0;
}
